package cn.ante.basic.controller;

import cn.ante.basic.exception.BusinessException;
import cn.ante.basic.service.ILoginService;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.vo.BinderVo;
import cn.ante.basic.vo.LoginVo;
import cn.ante.basic.vo.WechatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
    * @Description: 后台登录接口
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/7/7 16:04
    */
    @PostMapping("/account")
    public AjaxResult adminLogin(@RequestBody LoginVo loginVo) {
        try {
            Map<String,Object> map = loginService.adminLogin(loginVo);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }

    @PostMapping("/wechat")
    public AjaxResult wechatLogin(@RequestBody WechatVo wechatVo) {
        try {
            return loginService.wechatLogin(wechatVo);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody BinderVo binderVo) {
        try {
            Map<String,Object> map = loginService.wechatBinder(binderVo);
            return AjaxResult.me().setResultObj(map);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }


}
